package createfile

import (
	"fmt"
	"os/exec"
	"unicode"

	// "golang_start/config/mysql"
	common "golang_start/common"
	"os"
	"path/filepath"
	"strings"

	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
)

// var DB = mysql.DB

type Fieldrows struct {
	Field   string
	Type    string
	Comment string
	Null    string
}

func init() {

}

/*测试*/
type Membertype struct {
	Id               uint `gorm:"primaryKey"`
	Typename         string
	Typeday          int64
	Type_img         string
	Member_price     float64
	Member_lineation float32
	Member_number    uint32
	Member_sort      uint
	Member_status    int16
	Member_content   string
	Create_name      string
	Created_at       int64 `gorm:"autoCreateTime"`
	Updated_at       int64 `gorm:"autoUpdateTime"`
	Deleted_at       int64
}

/*创建初始化*/

func Index(c *gin.Context) {
	fmt.Printf("进入到这里")
	// insert_data := Membertype{
	// 	Typename:         "测试",
	// 	Typeday:          52,
	// 	Type_img:         "123456",
	// 	Member_price:     85.63,
	// 	Member_lineation: 856.684,
	// 	Member_number:    58,
	// 	Member_sort:      58,
	// 	Member_status:    1,
	// 	Member_content:   "456789151515151515",
	// 	Create_name:      "aaaddas",
	// 	// Updated_at:       int(time.Nanosecond)
	// 	Deleted_at: time.Now().Unix(),
	// }
	// fmt.Println(time.Now().Unix())
	// common.DB.Create(&insert_data)

	// // common.DB.First(&Membertype_Model{})
	// fmt.Println(insert_data.Id)
	// c.JSON(200, gin.H{
	// 	"message": "进入到这里!" + string(insert_data.Id),
	// })
	// service.createFile()

	//  Db::select("select * from information_schema.tables where table_schema='".config('databases.default.database')."' ");
	// fmt.Println("mysql_配置")
	// fmt.Println(lib.GetStringConf("base.base.database_prefix"))
	// table_prefix := lib.GetStringConf("base.base.database_prefix")
	rows, err := common.DB.Raw("select TABLE_NAME,TABLE_COMMENT from information_schema.tables where table_schema = ?", "code_scanner").Rows()
	if err != nil {
		fmt.Println(err.Error())
	}
	var name, comment string
	type Tablerows struct {
		TABLE_NAME    string
		TABLE_COMMENT string
	}
	defer rows.Close()

	table_rows := []Tablerows{}
	for rows.Next() {
		rows.Scan(&name, &comment)
		// table_rows := &Tablerows{}
		var a = Tablerows{
			TABLE_NAME:    name,
			TABLE_COMMENT: comment,
		}
		// table_rows.Name = name
		// table_rows.Comment = comment
		table_rows = append(table_rows, a)
	}
	// return view('zz/index', ['row' => $row]);
	c.HTML(200, "index.blade.tmpl", gin.H{
		"title": "Main website",
		"rows":  table_rows,
	})

}

/*获取字段*/
func Cloumn(c *gin.Context) {
	table := c.Query("table")
	if table == "" {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "表名不能为空",
		})
	}
	fmt.Println(table)

	// $table = $request->input('table', '');
	//     $row = Db::select("show full columns from ".$table);
	//     return json_encode(['code'=>200,'msg'=>'success','data'=>$row],302);
	result, err := common.DB.Raw("show full columns from " + table).Rows()
	if err != nil {
		fmt.Println("错误！" + table)
	}
	defer result.Close()

	type TableColum struct {
		Field   string
		Type    string
		Comment string
		Key     string
	}
	var data = []TableColum{}

	for result.Next() {
		row := TableColum{}
		common.DB.ScanRows(result, &row)
		data = append(data, row)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "okokok",
		"data": data,
	})

}

func Pathisexists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsExist(err) {
		return true
	}
	return false
}

func CreateDirectory(path string) {
	cmd := exec.Command("mkdir", "-p", path)
	out, err := cmd.CombinedOutput()

	if err != nil {
		fmt.Printf("CreateDirectory failed: %v\n", err)
	} else {
		fmt.Println("Created directory: ", out)
	}
}

/*创建文件*/
func Create(c *gin.Context) {
	// ex := exec.Command(`mkdir`, `test12a`)
	// err := ex.Run()
	// CreateDirectory("test12aaa2")
	// // fmt.Println("cmd错误", err.Error())
	// return

	fmt.Printf("创建文件")
	model_old_content, err := os.ReadFile("./go_model.tmp")
	if err != nil {
		panic(err)
		fmt.Println(model_old_content)
		fmt.Println("错误：", err.Error())
	}

	table_name := c.PostForm("table")

	// table_prefix := mysql.Dbfix()

	// table_name = strings.Replace(table_name, table_prefix, "", 1)

	controller_file := c.PostForm("namespace")
	router_path := c.PostForm("routespace")
	service_path := c.PostForm("service")

	grpc_path := c.PostForm("grpcpackage")
	grpc_router_path := c.PostForm("grpcroutespace")
	grpcfilepath := c.PostForm("grpcfilepath")
	field_data := c.PostFormMap("field")
	// bytes, _ := ioutil.ReadAll(c.Request.Body)
	// fmt.Println(string(bytes))

	final_field_data := make(map[string]map[string]string)
	for k, _ := range field_data {

		// fmt.Println(k)
		final_field_data[k] = make(map[string]string)
		final_field_data[k]["comment"] = c.DefaultPostForm("field["+k+"][comment]", "")
		final_field_data[k]["isrequired"] = c.DefaultPostForm("field["+k+"][isrequired]", "off")
		final_field_data[k]["condition"] = c.DefaultPostForm("field["+k+"][condition]", "")
		final_field_data[k]["usetimechange"] = c.DefaultPostForm("field["+k+"][usetimechange]", "off")
		final_field_data[k]["usesoftdelete"] = c.DefaultPostForm("field["+k+"][usesoftdelete]", "off")
		/*外键关联*/
		final_field_data[k]["connect"] = c.DefaultPostForm("field["+k+"][connect]", "off")
		final_field_data[k]["model"] = c.DefaultPostForm("field["+k+"][model]", "")
		final_field_data[k]["foreign_key"] = c.DefaultPostForm("field["+k+"][foreign_key]", "")
		final_field_data[k]["foreign_many"] = c.DefaultPostForm("field["+k+"][foreign_many]", "off")

		/*type生成*/
		final_field_data[k]["istype"] = c.DefaultPostForm("field["+k+"][istype]", "off")

	}
	/*生成判断条件*/

	// check_str := get_require(final_field_data)

	condition_str := get_condition(final_field_data)
	connect_relation_str := get_relation(final_field_data)
	type_slice_str := create_type_slice(final_field_data)
	type_func_str := create_type_hook(final_field_data)
	// c.Request.ParseForm()
	// for k, v := range c.Request.PostForm {
	// 	fmt.Println(k)
	// 	fmt.Println(v)
	// }
	// fmt.Printf("123456")
	controller_path := controller_file
	if !Pathisexists(controller_file) {
		os.Mkdir(controller_path, 0666)
	}
	if !Pathisexists(service_path) {
		os.Mkdir(service_path, 0666)
	}

	if grpcfilepath != "" && !Pathisexists(service_path) {
		os.Mkdir(grpcfilepath, 0666)
	}

	service_package := filepath.Base(service_path)
	package_name := filepath.Base(controller_file)
	grpcpackage_name := filepath.Base(grpcfilepath)
	model_name := getModelName(table_name)
	// fmt.Println(string(model_old_content))
	// fmt.Println(filepath.Base(controller_file))
	// fmt.Println(filepath.Dir(controller_file))

	// model_content := strings.Replace(string(model_old_content), "{%package%}", package_name, -1)

	// /*替换模型名称*/
	// model_content = strings.ReplaceAll(model_content, "{%model_name%}", getModelName(table_name))
	// /*替换模型字段*/
	// model_content = strings.Replace(model_content, "{%model_fields%}", getFields(table_name), -1)

	// /*生成模型文件*/
	// _, ferr := os.OpenFile(table_name+".go", os.O_APPEND|os.O_CREATE, 0666)
	// if ferr != nil {
	// 	fmt.Println("模型生成失败", ferr.Error())
	// }
	// os.WriteFile(table_name+".go", []byte(model_content), 0666)
	struct_data := getFields(table_name, final_field_data)
	// fmt.Println(struct_data)
	// return
	/**控制器替换**/
	controller_old_content, errc := os.ReadFile("./go_controller.tmp")
	if errc != nil {
		panic(errc)
		fmt.Println("错误：", errc.Error())
	}
	controller_content := strings.Replace(string(controller_old_content), "{%package%}", package_name, -1)

	controller_content = strings.ReplaceAll(controller_content, "{%server_struct%}", service_package+"."+model_name)
	controller_content = strings.Replace(controller_content, "{%model_fields%}", struct_data, -1)
	controller_content = strings.Replace(controller_content, "{%struct_name%}", model_name, -1)
	/**生成控制器文件*/
	os.WriteFile(controller_path+"/"+table_name+".go", []byte(controller_content), 0666)

	/*替换路由*/
	/*{%routes%}*/
	if Pathisexists(router_path) {

		methods := []string{
			"Add_",
			"Delete_",
			"Update_",
			"List_",
			"Detail_",
		}
		router_str := getrouter(methods, table_name, package_name, model_name)
		router_content, err := os.ReadFile(router_path)

		if err != nil {
			fmt.Println("文件打开失败", err)
		}

		router_content = []byte(strings.ReplaceAll(string(router_content), "/*{%routes%}*/", router_str+"/*{%routes%}*/\r\n"))
		/*生成路由文件*/
		os.WriteFile(router_path, []byte(router_content), 0666)

	}
	/*创建service文件*/
	service_old_content, errc := os.ReadFile("./go_service.tmp")

	service_content := strings.ReplaceAll(string(service_old_content), "{%package%}", service_package)

	service_content = strings.Replace(service_content, "/*{%struct_type_func%}*/", type_func_str, -1)

	service_content = strings.Replace(service_content, "{%model_fields%}", struct_data, -1)
	service_content = strings.ReplaceAll(service_content, "{%struct_name%}", model_name)
	service_content = strings.Replace(service_content, "{%table_name%}", table_name, -1)
	service_content = strings.Replace(service_content, "/*{%where%}*/", condition_str, -1)
	service_content = strings.Replace(service_content, "/*{%connect_relation%}*/", connect_relation_str, -1)

	service_content = strings.Replace(service_content, "{%struct_type%}", type_slice_str, -1)

	/*创建service文件*/
	os.WriteFile(service_path+"/"+table_name+".go", []byte(service_content), 0666)
	// os.WriteFile(table_name+".go", []byte(service_content), 0666)

	// fmt.Println(string(controller_content))

	/*生成grpc*/
	grpc_file_path := "./grpc/grpc.proto"
	if Pathisexists(grpc_file_path) {

		grpc_service_name := coverNameToUnderline(model_name + "Grpc")

		grpc_tpl := get_grpc_tpl(grpc_service_name)
		grpc_routes_data, err := os.ReadFile(grpc_file_path)

		if err != nil {
			fmt.Println("文件打开失败", err)
		}

		grpc_routes_data = []byte(strings.ReplaceAll(string(grpc_routes_data), "/*{%grpc_auto_create%}*/", grpc_tpl+"/*{%grpc_auto_create%}*/\r\n"))
		/*生成GRPC文件*/
		os.WriteFile(grpc_file_path, []byte(grpc_routes_data), 0666)

		/**
		 * 自动执行创建命令 protoc --go_out=./grpc --go_opt=paths=import   --go-grpc_out=./grpc --go-grpc_opt=paths=import ./grpc/grpc.proto
		 */
		exec.Command("protoc", "--go_out=./grpc --go_opt=paths=import   --go-grpc_out=./grpc --go-grpc_opt=paths=import ./grpc/grpc.proto")

		/*生成Grpc路由*/

		if Pathisexists(grpc_router_path) {

			grpc_router_content, err := os.ReadFile(grpc_router_path)

			if err != nil {
				fmt.Println("文件打开失败", err)
			}
			grpc_router_str := grpc_path + ".Register" + getModelName(grpc_service_name) + "Server" + "(gs, &" + grpcpackage_name + "." + model_name + "Grpc{})"

			grpc_router_content = []byte(strings.ReplaceAll(string(grpc_router_content), "/*{%grpc_routes%}*/", grpc_router_str+"/*{%grpc_routes%}*/\r\n"))
			/*生成路由文件*/
			os.WriteFile(grpc_router_path, []byte(grpc_router_content), 0666)

		}

		/*生成grpc控制器*/
		controller_old_grpc_content, err := os.ReadFile("./go_grpc_controller.tmp")
		if err != nil {
			fmt.Println(err.Error())
		}
		controller_grpc_content := strings.ReplaceAll(string(controller_old_grpc_content), "{%struct_name%}", model_name)

		controller_grpc_content = strings.ReplaceAll(string(controller_grpc_content), "{%go_file_package_path%}", grpcpackage_name)

		controller_grpc_content = strings.ReplaceAll(string(controller_grpc_content), "/*{%grpcpackage%}*/", "")
		controller_grpc_content = strings.ReplaceAll(string(controller_grpc_content), "{%package_grpc_name%}", grpc_path)

		os.WriteFile(grpcfilepath+"/"+table_name+".go", []byte(controller_grpc_content), 0666)

	}

	c.JSON(200, gin.H{"code": 200, "msg": "成功创建文件"})
}

/*生成grpc*/
func get_grpc_tpl(server_name string) string {
	return `
		service ` + server_name + ` {
			rpc add_ (ParamRequest) returns (DataResponse) {
			}
			rpc delete_ (ParamRequest) returns (DataResponse) {
			}
			rpc update_ (ParamRequest) returns (DataResponse) {
			}
			rpc list_ (ParamRequest) returns (DataResponse) {
			}
			rpc detail_ (ParamRequest) returns (DataResponse) {
			}
		}
	`
}

/*生成判断*/
func get_require(field map[string]map[string]string) string {
	var str string = ""

	for k, v := range field {

		if v["isrequired"] == "on" {
			str += `binding:"required"`
		}
		fmt.Println(k)
		fmt.Println(v["isrequired"])
	}

	return str
}

/***生成type***/
func get_type(field map[string]map[string]string) string {
	str := ""
	for k, v := range field {

		if v["istype"] == "on" {
			str += (getModelName(k + "_text")) + " string `gorm:\"-\" json:\"" + k + "_text\"`\r\n"
		}

	}
	return str
}

func create_type_slice(field map[string]map[string]string) string {
	// str :=`var StatusTextType = map[string]string{"1": "开启1", "0": "结束"}`;
	str := ""
	for k, v := range field {

		if v["istype"] == "on" && v["comment"] != "" {
			comment_str := v["comment"]
			// fmt.Println(comment_str)
			// split_str := strings.LastIndex(comment_str, ":")
			// comment_str_rune := []rune(comment_str)
			// comment_str_final := comment_str_rune[(split_str + 1):]
			// fmt.Println(string(comment_str_final))
			_, after_str, _ := strings.Cut(comment_str, ":")
			fmt.Println(after_str)
			comment_arr := strings.Split(string(after_str), ",")
			fmt.Println(comment_arr)
			if len(comment_arr) > 0 {
				mapstring := ""
				for _, v := range comment_arr {
					real_val := strings.Split(v, "=")
					fmt.Println(real_val)
					if len(real_val) == 2 {
						mapstring += `"` + real_val[0] + `":"` + real_val[1] + `",`
					}
				}
				str += "//" + v["comment"] + " \r\n"
				str += "var " + (getModelName(k + "_text_type")) + " = map[string]string{" + mapstring + "}\r\n"
			}

		}

	}
	return str
}

/*生成type钩子函数**/
func create_type_hook(field map[string]map[string]string) string {
	// str := `func (model *ScWebLivemessage) AfterFind(tx *gorm.DB) error {`
	var is_type_exists = false
	str := `func (model *{%struct_name%}) AfterFind(tx *gorm.DB) error {
		var vt, val, status_val string
		var ok bool
		`
	for k, v := range field {

		if v["istype"] == "on" {
			is_type_exists = true
			model_name := getUppercase(k)
			model_slice := getModelName(k + "_text_type")
			str += `
			vt = reflect.ValueOf(model.` + model_name + `).Kind().String()
			if vt == "int" {
				status_val = strconv.FormatInt(int64(model.` + model_name + `), 10)
			} else {
				status_val = ` + model_slice + `[string(model.` + model_name + `)]
			}
			val, ok = ` + model_slice + `[status_val]
			if ok {
				model.` + getModelName(k+"_text") + ` = val
			}
			`
		}
	}

	str += `
		return nil
	}`
	if !is_type_exists {
		str = ""
	}
	return str

}

/*生成关联*/

func get_relation(field map[string]map[string]string) string {
	str := ""
	has := 0
	table_prefix := lib.GetStringConf("base.base.database_prefix")
	for _, v := range field {
		if v["connect"] == "on" {

			model_str := []rune(v["model"])
			last_index := strings.LastIndex(v["model"], "/")
			foreign_model_name := model_str[(last_index + 1):]

			foreign_model_name_str_Lower := coverNameToUnderline(string(foreign_model_name))

			Column_field := strings.Replace(foreign_model_name_str_Lower, table_prefix, "", 1)
			fieldName := getModelName(Column_field)
			if has == 0 {
				str += `result = result.Preload("` + fieldName + `")`
			} else {
				str += `.Preload("` + fieldName + `")`
			}
			has++
		}
	}

	return str
}

/*生成查询条件*/

func get_condition(field map[string]map[string]string) string {
	var str string = ""

	// if k == "typename" {
	// 	fmt.Println("%" + (string(v[0])) + "%")
	// 	result = result.Where("typename LIKE (?)", "%"+v[0]+"%")
	// }

	str += "data_param := c.Request.URL.Query()\n\r"

	str += "for k, v := range data_param {\n\r"
	has := false
	for k, v := range field {

		if v["condition"] == "equal" {
			str += `if k == "` + k + `"{` + "\n\r"
			str += `result = result.Where("` + k + ` = (?)",v[0])` + "\n\r"
			str += `}` + "\n\r"
			has = true
		}

		if v["condition"] == "like" {
			str += `if k == "` + k + `"{` + "\n\r"
			str += `result = result.Where("` + k + ` LIKE (?)","%"+v[0]+"%")` + "\n\r"
			str += `}` + "\n\r"
			has = true
		}
		if v["condition"] == "between" {
			str += `if k == "` + k + `"{` + "\n\r"
			str += `result = result.Where("` + k + ` BETWEEN ? AND ?",v[0],v[1])` + "\n\r"
			str += `}` + "\n\r"
			has = true
		}
	}
	str += "}\n\r"
	if !has {
		str = ""
	}
	return str
}

/*路由生成*/
func getrouter(methods []string, router_name string, package_name string, struct_name string) string {
	var router_str string
	up_table_name := getUppercase(router_name)
	router_str += up_table_name + " := g.Group(\"/" + router_name + "\")\r\n"
	router_str += "{\r\n"
	MType := "POST"
	for _, v := range methods {
		fmt.Println(v)
		if v == "List_" || v == "Detail_" {
			MType = "GET"
		}
		router_str += up_table_name + "." + MType + "(\"/" + getlowercase(v) + "\", (&" + package_name + "." + struct_name + "{})." + v + ")\r\n"
	}
	router_str += "}\r\n"
	return router_str
}

func getFields(table_name string, field map[string]map[string]string) string {
	var model_fields = ""
	sql := "show full columns from " + table_name
	rows, err := common.DB.Raw(sql).Rows()
	if err != nil {
		panic(err)
	}
	// result := []Fieldrows{}
	var r = Fieldrows{}
	for rows.Next() {
		common.DB.ScanRows(rows, &r)
		field_type := gettypes(r, field)
		field_column, usetype := getFieldcolumns(string(r.Field), field)
		// fmt.Println(field_column)
		// fmt.Println(field_type)
		if field_column != "" && usetype == 1 {
			model_fields += getUppercase(r.Field) + " " + field_type + " " + field_column + "\n\r"
		} else if usetype == 0 {
			model_fields += getUppercase(r.Field) + " " + field_column + " //" + r.Comment + " \n\r"
		} else if usetype == 2 {
			model_fields += field_column + " //关联字段 \n\r"
			model_fields += getUppercase(r.Field) + " " + field_type + " `gorm:\"column:" + r.Field + "\" json:\"" + r.Field + "\" form:\"" + r.Field + "\"`" + " //" + r.Comment + " \n\r"
		} else {
			model_fields += getUppercase(r.Field) + " " + field_type + " `gorm:\"column:" + r.Field + "\" json:\"" + r.Field + "\" form:\"" + r.Field + "\"`" + " //" + r.Comment + " \n\r"
		}
	}
	/*替换字符串*/
	model_fields += get_type(field)
	fmt.Println(model_fields)
	return model_fields

}

/*数据库字段类型转换*/
func gettypes(typestr Fieldrows, field map[string]map[string]string) string {
	type_str := "string"
	if (strings.Index(typestr.Type, "char") >= 0) || (strings.Index(typestr.Type, "time") >= 0) {
		type_str = "string"
	}

	if (strings.Index(typestr.Type, "int") >= 0) || (strings.Index(typestr.Type, "tinyint") >= 0) {
		type_str = "int"
	}
	if strings.Index(typestr.Type, "text") >= 0 {
		type_str = "string"
	}

	if (strings.Index(typestr.Type, "float") >= 0) || (strings.Index(typestr.Type, "decimal") >= 0) {
		type_str = "float64"
	}

	return type_str
}

/*类型映射*/
func getFieldcolumns(fieldstr string, field map[string]map[string]string) (str string, usetype int) {
	str = ""
	usetype = 1
	table_prefix := lib.GetStringConf("base.base.database_prefix")
	if fieldstr == "id" {
		str = "`gorm:\"primaryKey\" form:\"id\" json:\"id\"`"
	}
	if fieldstr == "created_at" {
		str = "`gorm:\"autoCreateTime\" form:\"created_at\"`"
	}
	if fieldstr == "updated_at" {
		str = "`gorm:\"autoUpdateTime\" form:\"updated_at\"`"
	}
	if fieldstr == "deleted_at" {
		for k, v := range field {
			if k == "deleted_at" && v["usesoftdelete"] == "on" {
				str = "gorm.DeletedAt `gorm:\"deleted_at\" json:\"deleted_at\" comment:\"数据库类型必须为：datetime时间格式为2006-01-02 15:04:05\"`"
				usetype = 0
			}
		}
	}

	for k, v := range field {
		if k == fieldstr && v["usetimechange"] == "on" {
			str = "{%struct_name%}Parsertime `gorm:\"column:" + fieldstr + "\" json:\"" + fieldstr + "\" form:\"" + fieldstr + "\" comment:\"时间转换字段-" + v["comment"] + "\"`"
			usetype = 0
		}

		if k == fieldstr && v["connect"] == "on" {
			usetype = 2
			fmt.Println("开启关联")
			fmt.Println(v["model"])
			model_str := []rune(v["model"])
			last_index := strings.LastIndex(v["model"], "/")
			foreign_model_name := model_str[(last_index + 1):]

			package_name := model_str[0:last_index]
			foreign_model_name_str := string(foreign_model_name)
			fmt.Println(string(foreign_model_name))
			package_name_arr := strings.Split(string(package_name), "/")

			package_name_str := package_name_arr[len(package_name_arr)-1]
			if package_name_str == "service" {
				package_name_str = ""
			} else {
				package_name_str += "."
			}
			fmt.Println(package_name_arr[len(package_name_arr)-1])

			foreign_model_name_str_Lower := coverNameToUnderline(foreign_model_name_str)
			Column_field := strings.Replace(foreign_model_name_str_Lower, table_prefix, "", 1)
			fmt.Println(foreign_model_name_str_Lower)
			fmt.Println("end--------------------------")
			hasmany := ""
			if v["foreign_many"] == "on" {
				hasmany = `[]`
			}
			str = package_name_str + getModelName(Column_field) + " " + hasmany + string(foreign_model_name) + " `gorm:\"foreignkey:" + v["foreign_key"] + ";references:" + fieldstr + "\" json:\"" + Column_field + "\"`"
		}

	}

	//
	// result = result.Model(&ScArticle{}).Preload("ArticleCategory")
	// result.Preload("ArticleCategory")

	return str, usetype
}

/*转换*/
func coverNameToUnderline(str string) string {
	strrune := []rune(str)
	newstr := ""
	for _, v := range strrune {
		if unicode.IsUpper(v) {
			newstr += "_" + strings.ToLower(string(v))
		} else {
			newstr += strings.ToLower(string(v))
		}
	}
	return strings.TrimLeft(newstr, "_")
}
func getModelName(tablename string) string {
	str := ""
	tablename_arr := strings.Split(tablename, "_")

	for _, v := range tablename_arr {
		str += strings.Title(v)
	}
	return str
}

/*首字母大写*/
func getUppercase(name string) string {

	name = strings.Title(name)

	return name
}

/*转为小写*/
func getlowercase(name string) string {
	name = strings.ToLower(name)
	return name
}
